<?php

namespace app\controller\notify;

use app\constant\CommonConstant;
use app\constant\ModelConstant;
use app\libs\PayoneerSDK\Action\Payment;
use app\model\Notify;
use app\model\Orders;
use think\helper\Arr;
use think\Request;

class NotifyPayoneer implements NotifyInterface
{

    public function notify(Request $request)
    {
        $order_id = $request->param('order_id',0);
        $order = Orders::findOrEmpty($order_id);
        if($order->isEmpty()) throw new \Exception("order not found");
        return $order;
    }

    public function capture(Request $request, Orders $order)
    {
        //请求PKa接口详情
        $payoneer = new Payment($order->payment);
        $infoResult = $payoneer->getListDetail($order->order_no);
        $params = [
            'listId' => $order->order_no,
            'response' => $infoResult
        ];
        Notify::saveParams($order->id, $params, Notify::TYPE_PAYPAL_DETAIL);
        if (empty($infoResult)) throw new \Exception('payoneer info err');
        //异常账号信息保存
        $status = Arr::get($infoResult, 'status',[]);
        $code = Arr::get($status, 'code','-');
        $reason = Arr::get($status,'reason', '-');
        dump($code,$status,$reason);
        if ($reason == 'fraud-suspected') {
            $order->payment->status = ModelConstant::STATUS_OFF;
            $order->payment->save();
        }
        if($code == 'ended' && $reason == 'list_used'){
            Orders::completed($order,$code);
        }else{
            Orders::saveExceptionMessage($code,$reason);
        }
        return true;
    }
}